import 'package:flutter/cupertino.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';
import 'package:mobile_app.core/core/presentation/widgets/dialogs/cupertino_opaque_dialogs.dart';

class AppAlertDialog extends StatelessWidget {
  const AppAlertDialog({
    required this.actions,
    this.title,
    this.message,
    this.scrollController,
    this.actionScrollController,
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
    super.key,
  });

  final String? title;
  final String? message;
  final List<AppAlertDialogAction> actions;
  final ScrollController? scrollController;
  final ScrollController? actionScrollController;
  final Duration insetAnimationDuration;
  final Curve insetAnimationCurve;

  @override
  Widget build(BuildContext context) {
    Widget? titleWidget;
    if (title != null) {
      titleWidget = Text(
        title!,
        style: CoreTheme.of(context).textTheme.alertDialogTitle,
      );
    }

    Widget? messageWidget;
    if (message != null) {
      messageWidget = Text(
        message!,
        style: CoreTheme.of(context).textTheme.alertDialogMessage,
      );

      if (title != null) {
        messageWidget = Padding(
          padding: const EdgeInsets.only(
            top: 6,
          ),
          child: messageWidget,
        );
      }
    }

    return CupertinoOpaqueAlertDialog(
      title: titleWidget,
      content: messageWidget,
      actions: actions,
      scrollController: scrollController,
      actionScrollController: actionScrollController,
      insetAnimationDuration: insetAnimationDuration,
      insetAnimationCurve: insetAnimationCurve,
    );
  }
}

class AppAlertDialogAction extends StatelessWidget {
  const AppAlertDialogAction({
    required this.title,
    required this.onPressed,
    this.isDefaultAction = false,
    this.isDestructiveAction = false,
    super.key,
  });

  final String title;
  final VoidCallback onPressed;
  final bool isDefaultAction;
  final bool isDestructiveAction;

  @override
  Widget build(BuildContext context) {
    var textStyle = CoreTheme.of(context).textTheme.alertDialogAction;

    if (isDestructiveAction) {
      textStyle = textStyle.copyWith(
        color: CupertinoDynamicColor.resolve(CupertinoColors.systemRed, context),
      );
    }

    if (isDefaultAction) {
      textStyle = textStyle.copyWith(
        fontWeight: FontWeight.w600,
      );
    }

    return CupertinoOpaqueDialogAction(
      isDefaultAction: isDefaultAction,
      isDestructiveAction: isDestructiveAction,
      onPressed: onPressed,
      textStyle: textStyle,
      child: Text(title),
    );
  }
}
